package com.nj.api.web.controller;

import com.nj.api.entity.User;
import com.nj.api.service.AccountService;
import com.nj.api.service.BidInfoService;
import com.nj.api.service.ProductService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/bid")
public class BidInfoController {

    @Resource
    private ProductService productService;
    @Resource
    private BidInfoService bidInfoService;
    @Resource
    private AccountService accountService;
    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/records/{id}")
    public List records(@PathVariable Long id) {
        return bidInfoService.getRecords(id);
    }
    @PostMapping("/investment")
    private Map investment(BigDecimal bidMoney, Integer pid, @RequestHeader String token){//投资
        User user = (User) redisTemplate.opsForValue().get(token);
        accountService.investment(bidMoney,pid,user.getId());

        return new HashMap(){{
            put("success",true);
            put("msg","投资成功");
        }};
    }

    @GetMapping("/recordsForUser")
    //显示个人主页最近投资
    private List recordsForUser(@RequestHeader String token){
        User user = (User) redisTemplate.opsForValue().get(token);
        return bidInfoService.getByUserId(user.getId());
    }
}
